import { store } from "@/store";
import { defineStore } from "pinia";
import SubjectApi, {
  type CommissiontListParams,
  type CommissiontType,
} from "@/api/finance/commission";

export const useCommissionStore = defineStore("commission", () => {
  const list = ref<CommissiontType[]>([]);

  const totalCount = ref(0);

  const totalPage = ref(0);

  const loading = reactive({
    list: false,
    create: false,
    update: false,
    delete: false,
  });

  async function fetchList(params: CommissiontListParams) {
    loading.list = true;
    const response = await SubjectApi.getCommissiontList(params);
    loading.list = false;
    if (response) {
      list.value = response.records;
      totalCount.value = response.total_count;
      totalPage.value = response.total_page;
    }
  }

  return {
    list,
    totalCount,
    totalPage,
    loading,
    fetchList,
  };
});

export const useCommissionStoreHook = () => {
  return useCommissionStore(store);
};
